#!/bin/bash
################### 检测stock_python http服务是否存活脚本 #####################
# 执行之前：
# 	1. 去除特殊字符：sed -i 's/\r//' check_stock_http.sh
# 	2. 添加执行权限：chmod +x check_stock_http.sh
# 执行命令：./check_stock_http.sh
# 执行之后：
# 脚本说明：
########################################################
cd /home/stock_python/src
date=$(date +%Y-%m-%dT%H:%M:%S)
echo "当前时间为: $date" >> check_stock_http.log
stock_main_counter=$(ps -ef|grep stock_main | wc -l)
echo "stock_main_counter1: $stock_main_counter" >> check_stock_http.log
if [ "${stock_main_counter}" -lt 2 ]; then
  echo "stock_main is not alive, try starting stock_main" >> check_stock_http.log
  cd /home/stock_python/src
  ./start.sh
  sleep 10
  stock_main_counter=$(ps -ef|grep stock_main  | wc -l)
  echo "stock_main_counter2: $stock_main_counter" >> check_stock_http.log
  if [ "${stock_main_counter}" -lt 2 ]; then
    echo "stock_main is still not alive, try again after 30 seconds " >> check_stock_http.log
    sleep 30
    ./start.sh
  else
    echo "stock_main is already alive!" >> check_stock_http.log
  fi
else 
  echo "stock_main is still alive, check http ..." >> check_stock_http.log
  url=http://127.0.0.1:7000/index
  status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
  echo "http status_code1: $status_code" >> check_stock_http.log
  if [ $status_code -ne 200 ]; then
     ./restart.sh
     sleep 10
     status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
     echo "http status_code2: $status_code" >> check_stock_http.log
  else
     echo "http service is ok!" >> check_stock_http.log
  fi
fi